
[dbo].[asi_IsVatTaxableRegistration2]
CREATE PROCEDURE [dbo].[asi_IsVatTaxableRegistration2]
@eventCode varchar(10),
@registrantCountry varchar(25),
@registrantIsCompany bit,
@registrantIsVatRegistered bit,
@registrantVatCountry varchar(25)='',
@applyVat bit = NULL OUT
AS
BEGIN
DECLARE @orgCode varchar(5)
DECLARE @orgCountryCode varchar(2)
DECLARE @registrantCountryCode varchar(2)
DECLARE @ruleSet varchar(10)
DECLARE @isEuCountry bit
DECLARE @taxOnShipTo bit
DECLARE @shipToVatCountryCode varchar(25)
SELECT @taxOnShipTo = CASE WHEN [ShortValue]='YES' THEN 1 ELSE 0 END
FROM [dbo].[System_Params]
WHERE [ParameterName] = 'AR_Control.TaxOnShipTo'
SET @applyVat = 1
IF NOT EXISTS (SELECT 1
FROM [dbo].[LicenseLegacyList]
WHERE [LegacyLicenseCode]='VAT')
BEGIN
GOTO ReturnValue
END
IF LEN(ISNULL(@eventCode, ''))=0
BEGIN
GOTO ReturnValue
END
SELECT @orgCode = [ORG_CODE], @ruleSet = [VAT_RULESET]
FROM [dbo].[Meet_Master]
WHERE [MEETING] = @eventCode
IF @@ROWCOUNT=0
BEGIN
GOTO ReturnValue
END
IF LEN(ISNULL(@ruleSet, ''))=0
BEGIN
GOTO ReturnValue
END
IF LEN(ISNULL(@orgCode, ''))=0
BEGIN
SELECT @orgCode = o.[OrgCode], @orgCountryCode = c.[COUNTRY_CODE]
FROM [dbo].[Org_Control] o
INNER JOIN [dbo].[Country_Names] c on o.[VATCountry]=c.[VAT_COUNTRY_CODE]
WHERE o.[DefaultFlag]=1 AND o.[UseVATTaxation]=1 AND LEN(o.[VATCountry])>0
END
ELSE
BEGIN
SELECT @orgCountryCode = c.[COUNTRY_CODE]
FROM [dbo].[Org_Control] o
INNER JOIN [dbo].[Country_Names] c on o.[VATCountry]=c.[VAT_COUNTRY_CODE]
WHERE o.[OrgCode]=@orgCode AND o.[UseVATTaxation]=1 AND LEN(o.[VATCountry])>0
END
IF @@ROWCOUNT = 0
BEGIN
GOTO ReturnValue
END
SET @registrantCountryCode = NULL
IF LEN(ISNULL(@registrantCountry, ''))=0
BEGIN
SELECT @registrantCountryCode=c.[COUNTRY_CODE], @shipToVatCountryCode = c.[VAT_COUNTRY_CODE]
FROM [dbo].[Org_Control] o
INNER JOIN [dbo].[Country_Names] c on o.[VATCountry]=c.[VAT_COUNTRY_CODE]
WHERE o.[DefaultFlag]=1 AND o.[UseVATTaxation]=1 AND LEN(o.[VATCountry])>0
IF @@ROWCOUNT=0
BEGIN
SET @registrantCountryCode='US'
SET @shipToVatCountryCode=''
END
END
ELSE
BEGIN
IF LEN(@registrantCountry)<=2
BEGIN
SELECT @registrantCountryCode = [COUNTRY_CODE], @shipToVatCountryCode = [VAT_COUNTRY_CODE]
FROM [dbo].[Country_Names]
WHERE [COUNTRY_CODE]=@registrantCountry
END
ELSE
BEGIN
SELECT @registrantCountryCode = [COUNTRY_CODE], @shipToVatCountryCode = [VAT_COUNTRY_CODE]
FROM [dbo].[Country_Names]
WHERE [COUNTRY]=@registrantCountry
END
IF @@ROWCOUNT=0
BEGIN
GOTO ReturnValue
END
END
IF @taxOnShipTo=1 AND @registrantIsVatRegistered = 1
BEGIN
SELECT @registrantIsVatRegistered = CASE WHEN LEN(ISNULL(@registrantVatCountry, ''))>0 AND @registrantVatCountry=@shipToVatCountryCode THEN 1 ELSE 0 END
END
IF EXISTS (SELECT 1
FROM [dbo].[Country_Names]
WHERE [COUNTRY_CODE]=@registrantCountryCode AND [MAIL_GROUP]='EU')
BEGIN
SET @isEuCountry = 1
END
ELSE
BEGIN
SET @isEuCountry = 0
END
SELECT TOP 1 @applyVat = [VAT_APPLIES]
FROM [dbo].[Vat_Rule]
WHERE [VAT_RULESET]=@ruleSet
AND ([VAT_REGISTERED]=CASE @registrantIsVatRegistered WHEN 1 THEN 'Y' ELSE 'N' END OR [VAT_REGISTERED]='')
AND ([COMPANY_CONTACT]=CASE @registrantIsCompany WHEN 1 THEN 'Y' ELSE 'N' END OR [COMPANY_CONTACT]='')
AND (([LOCATION]='Same' AND @registrantCountryCode=@orgCountryCode)
OR
([LOCATION]='EU' AND @isEuCountry=1 AND @registrantCountryCode<>@orgCountryCode)
OR
([LOCATION]='NonEU' AND @isEuCountry=0)
OR
[LOCATION]=''
)
ORDER BY [RULE_ORDER]
IF @@ROWCOUNT = 0
BEGIN
SET @applyVat = 1
END
ReturnValue:
SELECT @applyVat AS [ApplyVat]
END
GO